﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Autodesk.Revit.DB;
using YArchitech.LIB;
using YArchitech.Revit;
using HYRevitCode.BoundingBoxUtility;
using HYRevitCode.RevitTopologySuite;
using HYRevitCode.XYZUtility;
using QuickModel.InteractiveOperation;
using Transfer.Utils;

namespace QuickModel.ColumnTransModel
{
	public class ColumnManager
	{
		public static Dictionary<ColumnConfigInfo, List<ColumnLoopInfo>> GroupColumn(List<CurveInfo> lstAllCurve, List<string> lstLayer)
		{
			Dictionary<ColumnConfigInfo, List<ColumnLoopInfo>> result = new Dictionary<ColumnConfigInfo, List<ColumnLoopInfo>>();
			try
			{
				Dictionary<string, List<CurveInfo>> dictionary = (from item in lstAllCurve
				group item by item.UseLayerName).ToDictionary((IGrouping<string, CurveInfo> item) => item.Key, (IGrouping<string, CurveInfo> item) => item.ToList<CurveInfo>());
				List<ColumnLoopInfo> list = new List<ColumnLoopInfo>();
				foreach (KeyValuePair<string, List<CurveInfo>> keyValuePair in dictionary)
				{
					bool flag = !lstLayer.Contains(keyValuePair.Key);
					if (!flag)
					{
						List<Curve> lstCurve = (from item in keyValuePair.Value
						select item.ThisCurve).ToList<Curve>();
						List<ColumnLoopInfo> columnLoop = ColumnManager.GetColumnLoop(lstCurve);
						list.AddRange(columnLoop);
					}
				}
				Dictionary<ColumnLoopInfo, ColumnConfigInfo> dictionary2 = new Dictionary<ColumnLoopInfo, ColumnConfigInfo>();
				foreach (ColumnLoopInfo columnLoopInfo in list)
				{
					ColumnConfigInfo value = new ColumnConfigInfo();
					ColumnManager.InitColumnConfig(ref value, columnLoopInfo);
					dictionary2.Add(columnLoopInfo, value);
				}
				IEnumerable<IGrouping<ColumnConfigInfo, ColumnLoopInfo>> source = dictionary2.GroupBy((KeyValuePair<ColumnLoopInfo, ColumnConfigInfo> item) => item.Value, (KeyValuePair<ColumnLoopInfo, ColumnConfigInfo> item) => item.Key, new ColumnConfigInfoEqualityComparer());
				result = source.OrderBy((IGrouping<ColumnConfigInfo, ColumnLoopInfo> item) => item.Key, new ComparerColumnConfigInfo()).ToDictionary((IGrouping<ColumnConfigInfo, ColumnLoopInfo> item) => item.Key, (IGrouping<ColumnConfigInfo, ColumnLoopInfo> pair) => pair.ToList<ColumnLoopInfo>());
			}
			catch (Exception)
			{
				throw;
			}
			return result;
		}

		public static FamilySymbol CreateColumnSymbol(Autodesk.Revit.DB.Document doc, ColumnConfigInfo config, ColumnLoopInfo columnLoopInfo)
		{
			FamilySymbol familySymbol = null;
			Transaction transaction = null;
			try
			{
				bool flag = config.TypeName != "<程序自选>";
				if (flag)
				{
					FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
					FilteredElementCollector filteredElementCollector2 = filteredElementCollector.OfClass(typeof(FamilySymbol));
					List<FamilySymbol> collection = filteredElementCollector2.OfCategory(BuiltInCategory.OST_StructuralColumns).Cast<FamilySymbol>().ToList<FamilySymbol>();
					filteredElementCollector = new FilteredElementCollector(doc);
					filteredElementCollector2 = filteredElementCollector.OfClass(typeof(FamilySymbol));
					List<FamilySymbol> collection2 = filteredElementCollector2.OfCategory(BuiltInCategory.OST_Columns).Cast<FamilySymbol>().ToList<FamilySymbol>();
					List<FamilySymbol> list = new List<FamilySymbol>();
					list.AddRange(collection);
					list.AddRange(collection2);
					familySymbol = list.Find((FamilySymbol item) => item.Family.Name + ":" + item.Name == config.TypeName);
				}
				else
				{
					BuiltInCategory builtInCategory = config.IsStruct ? -2001330 : -2000100;
					FilteredElementCollector filteredElementCollector3 = new FilteredElementCollector(doc);
					List<FamilySymbol> source = filteredElementCollector3.OfClass(typeof(FamilySymbol)).OfCategory(builtInCategory).Cast<FamilySymbol>().ToList<FamilySymbol>();
					List<Family> list2 = (from item in source
					group item by item.Family).ToList<IGrouping<Family, FamilySymbol>>().ConvertAll<Family>((IGrouping<Family, FamilySymbol> item) => item.Key);
					switch (config.ThisColumnCategory)
					{
					case EColumnCategory.eStrange:
					{
						string str = config.IsStruct ? ColumnDefaultSet.HYStructColumnFamilyTemplateName : ColumnDefaultSet.YJKArchColumnFamilyTemplateName;
						string strTemplateFilePath = Path.Combine(Product.InstallLocation, "Templates", RevitVersion.GetVersionNumber(doc), str + ".rft");
						StrangeColumnFamilySymbolCreator strangeColumnFamilySymbolCreator = new StrangeColumnFamilySymbolCreator(doc, columnLoopInfo.Pos, columnLoopInfo.Curves, strTemplateFilePath);
						string familyName = ColumnManager.GetFamilyName(EColumnCategory.eStrange);
						string uniqueFamilyName = ColumnManager.GetUniqueFamilyName(doc, familyName);
						string familySymbolName = ColumnManager.GetFamilySymbolName(config);
						familySymbol = strangeColumnFamilySymbolCreator.CreateStrangeColumnFamilySymbol(uniqueFamilyName, familySymbolName);
						break;
					}
					case EColumnCategory.eCircle:
					{
						transaction = new Transaction(doc, "创建柱类型");
						transaction.Start();
						string strFamilyName = ColumnManager.GetFamilyName(EColumnCategory.eCircle);
						Family family = list2.Find((Family item) => item.Name.Contains(strFamilyName));
						bool flag2 = family == null;
						if (flag2)
						{
							family = ColumnManager.LoadHYColumnFamily(doc, EColumnCategory.eCircle, config.IsStruct);
							string uniqueFamilyName2 = ColumnManager.GetUniqueFamilyName(doc, strFamilyName);
							family.Name = uniqueFamilyName2;
						}
						List<FamilySymbol> symbols = family.GetSymbols();
						FamilySymbol familySymbol2 = symbols.FirstOrDefault<FamilySymbol>();
						string strFamilySymbolName = ColumnManager.GetFamilySymbolName(config);
						familySymbol = symbols.Find((FamilySymbol item) => item.Name == strFamilySymbolName);
						bool flag3 = familySymbol == null;
						if (flag3)
						{
							familySymbol = (familySymbol2.Duplicate(strFamilySymbolName) as FamilySymbol);
							familySymbol.GetParameter("直径").Set(Common.MMToFeet(config.Diameter));
							ColumnManager.SetMaterial(doc, familySymbol);
						}
						transaction.Commit();
						break;
					}
					case EColumnCategory.eRectangle:
					{
						transaction = new Transaction(doc, "创建柱类型");
						transaction.Start();
						string strFamilyName = ColumnManager.GetFamilyName(EColumnCategory.eRectangle);
						Family family2 = list2.Find((Family item) => item.Name.Contains(strFamilyName));
						bool flag4 = family2 == null;
						if (flag4)
						{
							family2 = ColumnManager.LoadHYColumnFamily(doc, EColumnCategory.eRectangle, config.IsStruct);
							string uniqueFamilyName3 = ColumnManager.GetUniqueFamilyName(doc, strFamilyName);
							family2.Name = uniqueFamilyName3;
						}
						List<FamilySymbol> symbols2 = family2.GetSymbols();
						FamilySymbol familySymbol3 = symbols2.FirstOrDefault<FamilySymbol>();
						string strFamilySymbolName = ColumnManager.GetFamilySymbolName(config);
						familySymbol = symbols2.Find((FamilySymbol item) => item.Name == strFamilySymbolName);
						bool flag5 = familySymbol == null;
						if (flag5)
						{
							familySymbol = (familySymbol3.Duplicate(strFamilySymbolName) as FamilySymbol);
							familySymbol.GetParameter("宽度").Set(Common.MMToFeet(config.Width));
							familySymbol.GetParameter("深度").Set(Common.MMToFeet(config.Length));
							ColumnManager.SetMaterial(doc, familySymbol);
						}
						transaction.Commit();
						break;
					}
					}
				}
			}
			catch (Exception)
			{
				bool flag6 = transaction != null && transaction.HasStarted();
				if (flag6)
				{
					transaction.Commit();
				}
			}
			return familySymbol;
		}

		public static void PreLoadDefaultFamily(Autodesk.Revit.DB.Document doc, bool bStruct)
		{
			Transaction transaction = new Transaction(doc);
			try
			{
				transaction.Start("预加载族");
				BuiltInCategory builtInCategory = bStruct ? -2001330 : -2000100;
				FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
				List<FamilySymbol> source = filteredElementCollector.OfClass(typeof(FamilySymbol)).OfCategory(builtInCategory).Cast<FamilySymbol>().ToList<FamilySymbol>();
				List<Family> list = (from item in source
				group item by item.Family).ToList<IGrouping<Family, FamilySymbol>>().ConvertAll<Family>((IGrouping<Family, FamilySymbol> item) => item.Key);
				string strFamilyName = ColumnManager.GetFamilyName(EColumnCategory.eRectangle);
				Family family = list.Find((Family item) => item.Name.Contains(strFamilyName));
				bool flag = family == null;
				if (flag)
				{
					family = ColumnManager.LoadHYColumnFamily(doc, EColumnCategory.eRectangle, bStruct);
					string uniqueFamilyName = ColumnManager.GetUniqueFamilyName(doc, strFamilyName);
					family.Name = uniqueFamilyName;
				}
				strFamilyName = ColumnManager.GetFamilyName(EColumnCategory.eCircle);
				Family family2 = list.Find((Family item) => item.Name.Contains(strFamilyName));
				bool flag2 = family2 == null;
				if (flag2)
				{
					family2 = ColumnManager.LoadHYColumnFamily(doc, EColumnCategory.eCircle, bStruct);
					string uniqueFamilyName2 = ColumnManager.GetUniqueFamilyName(doc, strFamilyName);
					family2.Name = uniqueFamilyName2;
				}
				transaction.Commit();
			}
			catch (Exception)
			{
				bool flag3 = transaction.HasStarted();
				if (flag3)
				{
					transaction.RollBack();
				}
			}
		}

		private static List<ColumnLoopInfo> GetColumnLoop(List<Curve> lstCurve)
		{
			List<ColumnLoopInfo> list = new List<ColumnLoopInfo>();
			try
			{
				List<Curve> list2 = lstCurve.FindAll((Curve item) => (item is Arc || item is Ellipse) && !item.IsBound);
				foreach (Curve item3 in list2)
				{
					List<Curve> list3 = new List<Curve>();
					list3.Add(item3);
					list.Add(new ColumnLoopInfo
					{
						Curves = list3
					});
				}
				List<Curve> lstInputCurves = lstCurve.FindAll((Curve item) => (!(item is Arc) && !(item is Ellipse)) || item.IsBound);
				RevitPolygonFinder revitPolygonFinder = new RevitPolygonFinder();
				List<RevitPolygonDataBean> list4 = null;
				List<RevitPolygonDataBean> list5 = null;
				bool flag = revitPolygonFinder.TryGetPolygon(lstInputCurves, out list4, out list5, false, 3, false);
				if (flag)
				{
					List<CurveLoop> list6 = list4.ConvertAll<CurveLoop>((RevitPolygonDataBean item) => item.ShellCurveLoop);
					foreach (CurveLoop curveLoop in list6)
					{
						List<Curve> list7 = new List<Curve>();
						foreach (Curve item2 in curveLoop)
						{
							list7.Add(item2);
						}
						list.Add(new ColumnLoopInfo
						{
							Curves = list7
						});
					}
				}
			}
			catch (Exception)
			{
			}
			return list;
		}

		private static void InitColumnConfig(ref ColumnConfigInfo config, ColumnLoopInfo loop)
		{
			bool flag = config == null;
			if (flag)
			{
				config = new ColumnConfigInfo();
			}
			config.IsStruct = true;
			config.ThisColumnCategory = ColumnManager.RecorgniseShape(loop);
			switch (config.ThisColumnCategory)
			{
			case EColumnCategory.eStrange:
				loop.Pos = ColumnManager.GetStrangeColumnPos(loop.Curves);
				config.RefPos = loop.Pos;
				break;
			case EColumnCategory.eCircle:
			{
				Arc arc = loop.Curves[0] as Arc;
				config.Diameter = (double)((int)ColumnManager.FormatDimension(Common.FeetToMM(arc.Radius * 2.0)));
				loop.Pos = arc.Center;
				break;
			}
			case EColumnCategory.eRectangle:
			{
				Line line = loop.Curves[0] as Line;
				Line line2 = loop.Curves[1] as Line;
				double val = Math.Max(line.Length, line2.Length);
				double val2 = Math.Min(line.Length, line2.Length);
				config.Length = (double)((int)ColumnManager.FormatDimension(Common.FeetToMM(val)));
				config.Width = (double)((int)ColumnManager.FormatDimension(Common.FeetToMM(val2)));
				List<XYZ> intersection = ColumnManager.GetIntersection(loop.Curves.ConvertAll<Line>((Curve item) => item as Line));
				XYZ xyz2 = new XYZ(intersection.Average((XYZ xyz) => xyz.X), intersection.Average((XYZ xyz) => xyz.Y), intersection.Average((XYZ xyz) => xyz.Z));
				loop.Pos = xyz2;
				loop.Rotation = ColumnManager.GetRotationAngle(loop.Curves.ConvertAll<Line>((Curve item) => item as Line), xyz2);
				break;
			}
			}
		}

		private static EColumnCategory RecorgniseShape(ColumnLoopInfo loop)
		{
			try
			{
				bool flag = loop.Curves.IsEmpty<Curve>();
				if (flag)
				{
					return EColumnCategory.eStrange;
				}
				bool flag2 = ColumnManager.IsCircular(loop.Curves);
				if (flag2)
				{
					return EColumnCategory.eCircle;
				}
				bool flag3 = ColumnManager.IsRenctangle(loop.Curves);
				if (flag3)
				{
					return EColumnCategory.eRectangle;
				}
			}
			catch (Exception)
			{
			}
			return EColumnCategory.eStrange;
		}

		private static bool IsRenctangle(List<Curve> lstCurve)
		{
			bool flag = lstCurve.IsEmpty<Curve>() || lstCurve.Count != 4;
			bool result;
			if (flag)
			{
				result = false;
			}
			else
			{
				bool flag2 = false;
				List<Line> list = (from e in lstCurve
				where e is Line
				select e as Line).ToList<Line>();
				Line line = list.First<Line>();
				Line line2 = null;
				bool flag3 = false;
				ToleranceManger toleranceManger = ToleranceManger.GetToleranceManger();
				double tolerance = toleranceManger.GetTolerance("主体快模-柱：角度公差");
				double tolerance2 = toleranceManger.GetTolerance("主体快模-柱：距离公差");
				foreach (Line line3 in list)
				{
					bool flag4 = line3.Origin != line.Origin;
					if (flag4)
					{
						bool flag5 = Geometry.IsEqual(line3.Length, line.Length, tolerance2);
						if (flag5)
						{
							bool flag6 = XYZUtilityMethod.IsParallel(line3.Direction, line.Direction, tolerance);
							if (flag6)
							{
								line2 = line3;
								break;
							}
						}
					}
				}
				bool flag7 = line2 == null;
				if (flag7)
				{
					result = flag2;
				}
				else
				{
					foreach (Line line4 in list)
					{
						bool flag8 = Geometry.IsEqual(line4.Direction.AngleTo(line.Direction), Math.PI*.5, tolerance);
						if (flag8)
						{
							flag3 = true;
							break;
						}
					}
					bool flag9 = flag3;
					result = (flag9 || flag2);
				}
			}
			return result;
		}

		private static bool IsCircular(List<Curve> lstCurve)
		{
			bool result = false;
			bool flag = lstCurve.Count == 1 && lstCurve.First<Curve>() is Arc;
			if (flag)
			{
				Arc arc = lstCurve.First<Curve>() as Arc;
				bool flag2 = !arc.IsBound;
				if (flag2)
				{
					return true;
				}
			}
			return result;
		}

		private static List<XYZ> GetIntersection(List<Line> lstLine)
		{
			List<XYZ> list = new List<XYZ>();
			foreach (Line line in lstLine)
			{
				foreach (Line line2 in lstLine)
				{
					IntersectionResultArray intersectionResultArray = null;
					SetComparisonResult setComparisonResult = line.Intersect(line2, out intersectionResultArray);
					bool flag = intersectionResultArray != null && (from e in list
					where e.IsAlmostEqualTo(intersectionResultArray.get_Item(0).XYZPoint)
					select e).Count<XYZ>() == 0;
					if (flag)
					{
						list.Add(intersectionResultArray.get_Item(0).XYZPoint);
					}
				}
			}
			return list;
		}

		private static double GetRotationAngle(List<Line> lstLine, XYZ ptPos)
		{
			double num = 0.0;
			bool flag = lstLine.IsEmpty<Line>() || lstLine.Count != 4;
			double result;
			if (flag)
			{
				result = num;
			}
			else
			{
				lstLine.Sort(delegate(Line x, Line y)
				{
					bool flag3 = Geometry.LessThan(x.Length, y.Length);
					int result2;
					if (flag3)
					{
						result2 = -1;
					}
					else
					{
						bool flag4 = Geometry.GreaterThan(x.Length, y.Length);
						if (flag4)
						{
							result2 = 1;
						}
						else
						{
							result2 = 0;
						}
					}
					return result2;
				});
				XYZ endPoint = lstLine[0].GetEndPoint(0);
				XYZ endPoint2 = lstLine[0].GetEndPoint(1);
				XYZ endPoint3 = lstLine[1].GetEndPoint(0);
				XYZ endPoint4 = lstLine[1].GetEndPoint(1);
				XYZ xyz = endPoint + (endPoint2 - endPoint) / 2.0;
				XYZ xyz2 = endPoint3 + (endPoint4 - endPoint3) / 2.0;
				XYZ xyz3 = (xyz.Y > ptPos.Y) ? xyz : xyz2;
				XYZ xyz4 = xyz3 - ptPos;
				num = xyz4.AngleTo(XYZ.BasisY);
				bool flag2 = xyz4.X > 0.0;
				if (flag2)
				{
					num = -num;
				}
				result = num;
			}
			return result;
		}

		private static string GetRefDefaultHYColumnFamilyName(EColumnCategory eCategory, bool bStructure)
		{
			switch (eCategory)
			{
			case EColumnCategory.eCircle:
				if (bStructure)
				{
					return ColumnDefaultSet.HYStructCircleColumnFamilyName;
				}
				return ColumnDefaultSet.YJKArchCirlcleColumnFamilyName;
			case EColumnCategory.eRectangle:
				if (bStructure)
				{
					return ColumnDefaultSet.HYStructRectangleColumnFamilyName;
				}
				return ColumnDefaultSet.YJKArchRectangleColumnFamilyName;
			}
			return string.Empty;
		}

		private static string GetFamilyName(EColumnCategory eCategory)
		{
			string result = string.Empty;
			switch (eCategory)
			{
			case EColumnCategory.eStrange:
				result = "HY-异形柱";
				break;
			case EColumnCategory.eCircle:
				result = "HY-圆柱";
				break;
			case EColumnCategory.eRectangle:
				result = "HY-矩形柱";
				break;
			}
			return result;
		}

		private static string GetFamilySymbolName(ColumnConfigInfo config, string strMaterialName)
		{
			string result = string.Empty;
			switch (config.ThisColumnCategory)
			{
			case EColumnCategory.eStrange:
				result = strMaterialName;
				break;
			case EColumnCategory.eCircle:
				result = strMaterialName + "-" + ((int)config.Diameter).ToString();
				break;
			case EColumnCategory.eRectangle:
				result = string.Concat(new string[]
				{
					strMaterialName,
					"-",
					((int)config.Length).ToString(),
					"X",
					((int)config.Width).ToString()
				});
				break;
			}
			return result;
		}

		private static string GetFamilySymbolName(ColumnConfigInfo config)
		{
			string result = string.Empty;
			try
			{
				string columnMaterialName = ColumnDefaultSet.ColumnMaterialName;
				switch (config.ThisColumnCategory)
				{
				case EColumnCategory.eStrange:
					result = columnMaterialName;
					break;
				case EColumnCategory.eCircle:
					result = columnMaterialName + "-" + ((int)config.Diameter).ToString();
					break;
				case EColumnCategory.eRectangle:
					result = string.Concat(new string[]
					{
						columnMaterialName,
						"-",
						((int)config.Length).ToString(),
						"X",
						((int)config.Width).ToString()
					});
					break;
				}
			}
			catch (Exception)
			{
			}
			return result;
		}

		private static Family LoadHYColumnFamily(Autodesk.Revit.DB.Document doc, EColumnCategory eCategory, bool bStruct)
		{
			Family family = null;
			try
			{
				string strRefHYFamilyName = ColumnManager.GetRefDefaultHYColumnFamilyName(eCategory, bStruct);
				BuiltInCategory builtInCategory = bStruct ? -2001330 : -2000100;
				FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
				List<FamilySymbol> source = filteredElementCollector.OfClass(typeof(FamilySymbol)).OfCategory(builtInCategory).Cast<FamilySymbol>().ToList<FamilySymbol>();
				List<Family> source2 = (from item in source
				group item by item.Family).ToList<IGrouping<Family, FamilySymbol>>().ConvertAll<Family>((IGrouping<Family, FamilySymbol> item) => item.Key);
				Family family2 = null;
				family2 = source2.FirstOrDefault((Family thisItem) => thisItem.Name == strRefHYFamilyName);
				bool flag = family2 == null;
				if (flag)
				{
					string text = Path.Combine(Product.FamilyLocation, RevitVersion.GetVersionNumber(doc), ColumnDefaultSet.HYColumnFamilyFileDir, strRefHYFamilyName + ".rfa");
					doc.LoadFamily(text, out family);
				}
				else
				{
					try
					{
						family2.Name = strRefHYFamilyName + "-bak";
						string text2 = Path.Combine(Product.FamilyLocation, RevitVersion.GetVersionNumber(doc), ColumnDefaultSet.HYColumnFamilyFileDir, strRefHYFamilyName + ".rfa");
						doc.LoadFamily(text2, out family);
						family.Name = strRefHYFamilyName + "-new";
						family2.Name = strRefHYFamilyName;
					}
					catch (Exception)
					{
						family2.Name = strRefHYFamilyName;
						throw;
					}
				}
			}
			catch (Exception)
			{
				throw;
			}
			return family;
		}

		private static double FormatDimension(double dValue)
		{
			int num = 0;
			int a = (int)Math.Round(dValue);
			int num2 = Math.DivRem(a, 5, out num);
			bool flag = num < 3;
			double result;
			if (flag)
			{
				result = (double)(num2 * 5);
			}
			else
			{
				result = (double)(num2 * 5 + 5);
			}
			return result;
		}

		private static XYZ GetStrangeColumnPos(List<Curve> lstCurve)
		{
			XYZ result;
			try
			{
				List<BoundingBoxUV> list = new List<BoundingBoxUV>();
				foreach (Curve curve in lstCurve)
				{
					BoundingBoxUV boundingBoxUV = new BoundingBoxUV();
					XYZ endPoint = curve.GetEndPoint(0);
					XYZ endPoint2 = curve.GetEndPoint(1);
					UV min = new UV(Math.Min(endPoint.X, endPoint2.X), Math.Min(endPoint2.Y, endPoint2.Y));
					UV max = new UV(Math.Max(endPoint.X, endPoint2.X), Math.Max(endPoint2.Y, endPoint2.Y));
					boundingBoxUV.Max = max;
					boundingBoxUV.Min = min;
					list.Add(boundingBoxUV);
				}
				BoundingBoxUV boundingBoxUV2 = null;
				foreach (BoundingBoxUV boundingBoxUV3 in list)
				{
					bool flag = boundingBoxUV2 == null;
					if (flag)
					{
						boundingBoxUV2 = boundingBoxUV3;
					}
					else
					{
						boundingBoxUV2 = BoundingBoxUtilityMethod.BoundingBoxesMerge(boundingBoxUV2, boundingBoxUV3);
					}
				}
				UV uv = (boundingBoxUV2.Min + boundingBoxUV2.Max) / 2.0;
				XYZ xyz = new XYZ(uv.U, uv.V, 0.0);
				result = xyz;
			}
			catch (Exception)
			{
				result = null;
			}
			return result;
		}

		private static string GetUniqueFamilyName(Autodesk.Revit.DB.Document doc, string strFamilyName)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			List<Family> list = filteredElementCollector.OfClass(typeof(Family)).Cast<Family>().ToList<Family>();
			string strNewFamilyName = strFamilyName;
			Func<Family, bool> <>9__0;
			for (int i = 0; i < 2147483647; i++)
			{
				IEnumerable<Family> source = list;
				Func<Family, bool> matchFun;
				if ((matchFun = <>9__0) == null)
				{
					matchFun = (<>9__0 = ((Family item) => item.Name == strNewFamilyName));
				}
				bool flag = !source.Contains(matchFun);
				if (flag)
				{
					break;
				}
				strNewFamilyName = strFamilyName + "_" + i.ToString();
			}
			return strNewFamilyName;
		}

		public static Material GetMaterialObject(Autodesk.Revit.DB.Document doc, string strMaterialname)
		{
			try
			{
				FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
				ICollection<Element> collection = filteredElementCollector.OfClass(typeof(Material)).ToElements();
				foreach (Element element in collection)
				{
					bool flag = element.Name.CompareTo(strMaterialname) == 0;
					if (flag)
					{
						return element as Material;
					}
				}
			}
			catch (Exception)
			{
				throw;
			}
			return null;
		}

		private static void SetMaterial(Autodesk.Revit.DB.Document doc, FamilySymbol familySymbol)
		{
			Parameter parameter = familySymbol.GetParameter("材质");
			bool flag = parameter != null && !parameter.IsReadOnly;
			if (flag)
			{
				Material materialObject = ColumnManager.GetMaterialObject(doc, ColumnDefaultSet.ColumnMaterialName);
				bool flag2 = materialObject != null;
				if (flag2)
				{
					parameter.Set(materialObject.Id);
				}
			}
		}
	}
}
